import 'package:backend.core/core/domain/entities/google_play/google_play_one_time_purchased_item_transaction.dart';
import 'package:backend.purchases/domain/entities/google_play_api/google_play_api_revoked_purchased_item.dart';
import 'package:backend.purchases/domain/helpers/google_play/create_google_play_transaction_revocation_info_from_api_revoked_purchased_item.dart';

abstract class UpdateGooglePlayOneTimePurchasedItemTransactionFromRevokedPurchasedItem {
  GooglePlayOneTimePurchasedItemTransaction call({
    required GooglePlayOneTimePurchasedItemTransaction oneTimePurchasedItemTransaction,
    required GooglePlayApiRevokedPurchasedItem revokedPurchasedItem,
  });
}

class UpdateGooglePlayOneTimePurchasedItemTransactionFromRevokedPurchasedItemImpl
    implements UpdateGooglePlayOneTimePurchasedItemTransactionFromRevokedPurchasedItem {
  const UpdateGooglePlayOneTimePurchasedItemTransactionFromRevokedPurchasedItemImpl({
    required CreateGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem
        createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem,
  }) : _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem =
            createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem;

  final CreateGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem
      _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem;

  @override
  GooglePlayOneTimePurchasedItemTransaction call({
    required GooglePlayOneTimePurchasedItemTransaction oneTimePurchasedItemTransaction,
    required GooglePlayApiRevokedPurchasedItem revokedPurchasedItem,
  }) {
    if (oneTimePurchasedItemTransaction.orderId == revokedPurchasedItem.orderId) {
      final updatedRevocationInfo =
          _createGooglePlayTransactionRevocationInfoFromApiRevokedPurchasedItem(
        revokedPurchasedItem: revokedPurchasedItem,
      );

      return oneTimePurchasedItemTransaction.copyWith(
        revocationInfo: () => updatedRevocationInfo,
      );
    } else {
      return oneTimePurchasedItemTransaction;
    }
  }
}
